#!/bin/sh

# 检查网络工具
if ! type ping >/dev/null 2>&1; then
    echo '1.未安装net-tools';
    echo '开始安装net-tools';
    yum -y install net-tools
    echo 'net-tools安装完成';
else
    echo '1.已安装net-tools，跳过';
fi
if ! type wget >/dev/null 2>&1; then
    echo '未安装wget';
    echo '开始安装wget';
    yum -y install wget
    echo 'wget安装完成';
else
    echo '已安装wget，跳过';
fi

# 更新yum源
echo '2.开始更新yum源';
mkdir /etc/yum.repos.d/back
mv /etc/yum.repos.d/* /etc/yum.repos.d/back
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
yum repolist
yum -y update
echo '更新yum源完成';

# 安装python第三方库
echo '3.开始安装python第三方依赖库';
echo '/opt/local/vis/manager/etc/python/3th' > 3th.pth;

if [ -d "/usr/lib64/python2.7/site-packages" ]; then
    mv 3th.pth /usr/lib64/python2.7/site-packages
    elif [ -d "/usr/lib/python2.7/site-packages" ]; then
    mv 3th.pth /usr/lib/python2.7/site-packages
else
    echo "未找到python安装目录，异常退出";
fi
echo 'python第三方依赖库安装完成';

# 写入环境变量文件
echo '4.开始写入环境变量文件';
echo -e '
# 路径定义
export VIS_ROOT_PATH=/opt/local/vis
export VIS_LOG_PATH=/mnt/log/vis
export VIS_FILE_ROOT_PATH=/mnt/media/vis
export VIS_UPLOAD_PATH=/mnt/upload/vis
export VIS_LOG_FILE=/mnt/log/vis/vis.log
export VIS_PROFILE_PATH=~/.vis_profile

# 链接环境变量设置
export LD_LIBRARY_PATH=LD_LIBRARY_PATH:${VIS_ROOT_PATH}/manager/etc/ffmpeg/3th

# 端口定义
export SRS_RTMP_PORT=1935
export RESTFUL_HTTP_PORT=8100
export SRS_HTTP_PORT=8102
export NGINX_HTTP_PORT=8106

# 业务属性定义
export VIS_SERVICE_ROLE=
' > ~/.vis_profile
echo '环境变量文件写入完成';

# 关闭防火墙和selinux
echo '5.关闭防火墙';
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
echo '防火墙关闭完成';

# 解压程序包
echo '6.开始解压程序包';
cd dependencies
mkdir -p /opt/local
password='1234abcd!@#$@vmediax'
files=$(ls vis.*.upgrade)
for filename in $files
do
    openssl des3 -d -k ${password} -salt -in $filename | tar zxvf -
    cp -rf vis /opt/local
    chmod -R 755 /opt/local/vis
    break
done
rm -rf vis
echo '程序包解压完成';

# 运行程序
cd /opt/local/vis
./init-all.sh restart >/dev/null 2>&1

echo '7.安装成功';
